﻿/*
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "UpdatePage.h"
#include "CommandDlg.h"

//[-]PPA [Doxygen 1.5.1] #include "../client/SettingsManager.h"
// #include "../client/FavoriteManager.h"
#include "../ViewFeatures/AutoUpdate.h"


PropPage::TextItem UpdatePage::texts[] =
{
	{ IDC_AUTOUPDATE_USE, ResourceManager::AUTOUPDATE_ENABLE},
	{ IDC_AUTOUPDATE_RUNONSTARTUP, ResourceManager::AUTOUPDATE_RUNONSTARTUP},
	{ IDC_AUTOUPDATE_STARTATTIME, ResourceManager::AUTOUPDATE_STARTATTIME},
	{ IDC_AUTOUPDATE_URL_LABEL, ResourceManager::AUTOUPDATE_URL_LABEL},
	{ IDC_AUTOUPDATE_SCHEDULE_LABEL, ResourceManager::AUTOUPDATE_SCHEDULE_LABEL},
	{ IDC_AUTOUPDATE_COMPONENT_LABEL, ResourceManager::AUTOUPDATE_COMPONENT_LABEL},
	{ IDC_AUTOUPDATE_SERVER_RELEASE, ResourceManager::AUTOUPDATE_TO_RELEASE_SERV},
	{ IDC_AUTOUPDATE_SERVER_BETA, ResourceManager::AUTOUPDATE_TO_BETA_SERV },
	{ IDC_AUTOUPDATE_USE_CUSTOM_SERVER, ResourceManager::AUTOUPDATE_USE_CUSTOM_URL },
// End of Addition.
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};

PropPage::Item UpdatePage::items[] =
{
	{ IDC_AUTOUPDATE_USE, SettingsManager::AUTOUPDATE_ENABLE, PropPage::T_BOOL},
	{ IDC_AUTOUPDATE_RUNONSTARTUP, SettingsManager::AUTOUPDATE_RUNONSTARTUP, PropPage::T_BOOL},
	{ IDC_AUTOUPDATE_STARTATTIME, SettingsManager::AUTOUPDATE_STARTATTIME, PropPage::T_BOOL},
	{ IDC_AUTOUPDATE_URL, SettingsManager::AUTOUPDATE_SERVER_URL, PropPage::T_STR},
	{ IDC_AUTOUPDATE_USE_CUSTOM_SERVER, SettingsManager::AUTOUPDATE_USE_CUSTOM_URL, PropPage::T_BOOL},
// End of Addition.
	{ 0, 0, PropPage::T_END }
};

UpdatePage::ListItem UpdatePage::listItems[] =
{
	{ SettingsManager::AUTOUPDATE_SHOWUPDATEREADY, ResourceManager::AUTOUPDATE_SHOWUPDATEREADY}, // [+] SSA
	{ SettingsManager::AUTOUPDATE_UPDATE_UNKNOWN, ResourceManager::AUTOUPDATE_UPDATE_UNKNOWN}, // [+] SSA
	{ SettingsManager::AUTOUPDATE_FORCE_RESTART, ResourceManager::AUTOUPDATE_FORCE_RESTART}, // [+] SSA
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};

UpdatePage::ListItem UpdatePage::listComponents[] =
{
	{ SettingsManager::AUTOUPDATE_EXE, ResourceManager::AUTOUPDATE_EXE}, // [+] SSA
	{ SettingsManager::AUTOUPDATE_UTILITIES, ResourceManager::AUTOUPDATE_UTILITIES}, // [+] SSA
	{ SettingsManager::AUTOUPDATE_LANG, ResourceManager::AUTOUPDATE_LANG}, // [+] SSA
	{ SettingsManager::AUTOUPDATE_PORTALBROWSER, ResourceManager::AUTOUPDATE_PORTALBROWSER}, // [+] SSA
	{ SettingsManager::AUTOUPDATE_EMOPACKS, ResourceManager::AUTOUPDATE_EMOPACKS}, // [+] SSA
	{ SettingsManager::AUTOUPDATE_WEBSERVER, ResourceManager::AUTOUPDATE_WEBSERVER}, // [+] SSA
	// { SettingsManager::AUTOUPDATE_UPDATE_CHATBOT, ResourceManager::AUTOUPDATE_UPDATE_CHATBOT}, // [+] SSA - we don't update chat bot
	{ SettingsManager::AUTOUPDATE_SOUNDS, ResourceManager::AUTOUPDATE_SOUNDS}, // [+] SSA
	{ SettingsManager::AUTOUPDATE_ICONTHEMES, ResourceManager::AUTOUPDATE_ICONTHEMES}, // [+] SSA
	{ SettingsManager::AUTOUPDATE_COLORTHEMES, ResourceManager::AUTOUPDATE_COLORTHEMES}, // [+] SSA
	{ SettingsManager::AUTOUPDATE_DOCUMENTATION, ResourceManager::AUTOUPDATE_DOCUMENTATION}, // [+] SSA
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};

LRESULT UpdatePage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	PropPage::translate((HWND)(*this), texts);
	PropPage::read((HWND)*this, items, listItems, GetDlgItem(IDC_AUTOUPDATE_LIST));
	PropPage::read((HWND)*this, NULL, listComponents, GetDlgItem(IDC_AUTOUPDATE_COMPONENTS));
	
	
	ctrlTime.Attach(GetDlgItem(IDC_AUTOUPDATE_AT));
	WinUtil::GetTimeValues(ctrlTime);
	ctrlTime.SetCurSel(SETTING(AUTOUPDATE_TIME));
	ctrlTime.Detach();
	
	EnableAutoUpdate(BOOLSETTING(AUTOUPDATE_ENABLE));

	// [+] SSA
	if (BOOLSETTING(AUTOUPDATE_TO_BETA)) 
	{
		CheckDlgButton(IDC_AUTOUPDATE_SERVER_BETA, BST_CHECKED);
	}else
	{
		CheckDlgButton(IDC_AUTOUPDATE_SERVER_RELEASE, BST_CHECKED);
	}

	CheckUseCustomURL();
	// Do specialized reading here
	return TRUE;
}

void UpdatePage::write()
{
	PropPage::write((HWND)*this, items, listItems, GetDlgItem(IDC_AUTOUPDATE_LIST));
	PropPage::write((HWND)*this, NULL, listComponents, GetDlgItem(IDC_AUTOUPDATE_COMPONENTS));
	ctrlTime.Attach(GetDlgItem(IDC_AUTOUPDATE_AT));
	SET_SETTING(AUTOUPDATE_TIME, ctrlTime.GetCurSel());
	ctrlTime.Detach();
	
	SET_SETTING(AUTOUPDATE_TO_BETA,	IsDlgButtonChecked(IDC_AUTOUPDATE_SERVER_BETA) == BST_CHECKED );
}

LRESULT UpdatePage::onClickedUseAutoUpdate(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	EnableAutoUpdate(IsDlgButtonChecked(IDC_AUTOUPDATE_USE) == BST_CHECKED);
	
	return 0;
}
void UpdatePage::EnableAutoUpdate(BOOL isEnabled)
{
	::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_URL_LABEL), isEnabled);
	// ::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_URL), isEnabled);
	::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_SERVER_RELEASE), isEnabled);
	::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_SERVER_BETA), isEnabled);
	::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_SCHEDULE_LABEL), isEnabled);
	::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_RUNONSTARTUP), isEnabled);
	::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_STARTATTIME), isEnabled);
	::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_AT), isEnabled);
	::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_LIST), isEnabled);
	::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_COMPONENT_LABEL), isEnabled);
	::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_COMPONENTS), isEnabled);


}

LRESULT UpdatePage::OnClickedUseReleaseVersion(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if ( IsDlgButtonChecked(IDC_AUTOUPDATE_USE_CUSTOM_SERVER) == BST_UNCHECKED )
	{
		string updateURL = UPDATE_RELEASE_URL;
		GetDlgItem(IDC_AUTOUPDATE_URL).SetWindowText( Text::toT(updateURL).c_str());
	}
	return 0;
}
LRESULT UpdatePage::OnClickedUseBetaVersion(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if ( IsDlgButtonChecked(IDC_AUTOUPDATE_USE_CUSTOM_SERVER) == BST_UNCHECKED )
	{
		string updateURL = UPDATE_BETA_URL;
		GetDlgItem(IDC_AUTOUPDATE_URL).SetWindowText( Text::toT(updateURL).c_str());
	}
	return 0;
}

LRESULT UpdatePage::OnClickedUseCustomURL(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CheckUseCustomURL();

	return 0;
}

void UpdatePage::CheckUseCustomURL()
{
	if ( IsDlgButtonChecked(IDC_AUTOUPDATE_USE) == BST_CHECKED )
	{
		::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_URL), IsDlgButtonChecked(IDC_AUTOUPDATE_USE_CUSTOM_SERVER)  == BST_CHECKED);
		if (IsDlgButtonChecked(IDC_AUTOUPDATE_USE_CUSTOM_SERVER)  == BST_UNCHECKED )
		{
			BOOL bHandled = FALSE;
			if (IsDlgButtonChecked(IDC_AUTOUPDATE_SERVER_BETA) == BST_CHECKED )
			{
				OnClickedUseBetaVersion(0,0,0,bHandled);
			}else
			{
				OnClickedUseReleaseVersion(0,0,0,bHandled);
			}
		}
	}else
	{
		::EnableWindow(GetDlgItem(IDC_AUTOUPDATE_URL), FALSE);
	}
}

/**
 * @file
 * $Id: UpdatePage.cpp 7253 2011-06-01 21:14:08Z sa.stolper $
 */
